articles

Home / DeveloperSection / Articles / Radio Button in android

Radio Button in android

Nitish Kesarwani 2811 26-Dec-2017
 Description

By reading this article you will get a detailed description of a radio button and their purpose for being used Applications. It has two states either on/off. It is widely used in application, web forms to get information from the user.

In Radio button, there are so many options available in the application in web forms so that user can fill the appropriate field.

This article describes about two radio buttons and one onclick button which response when a user sends a feedback through radio button it produces a message on the screen through Toast. onChecked method is called when an input is given by the user clicking the button.

Attributes

o android:contentDescription: Attribute defines the description of the content item

o android: background: Drawable elements are used to set in the background

o android: visibility: It is used to set the visibility of controls

o android: onClick: Onclick handles the checked or unchecked state of a radio button

Methods

o toggle(): This method checking the status of a checked and unchecked radio button

o onClick(): It handles the user response when user checks the box


Android_Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.radiobutton">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Main_Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.mapsample.msclient009.radiobutton.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:gravity="center"
        android:text="Radio Button Description"
        android:textSize="20dp"/>
    <RadioGroup
        android:id="@+id/RadioGp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/rdbtn2"
            android:layout_width="391dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@android:string/no"
            android:textColorLink="@android:color/background_dark"
            tools:text="No" />
        <RadioButton
            android:id="@+id/rdbtn1"
            android:layout_width="364dp"
            android:layout_height="47dp"
            android:layout_weight="1"
            android:text="@android:string/yes"
            tools:text="Yes" />
    </RadioGroup>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onChecked"
        android:text="RadioButtonChecked" />
</LinearLayout>

Main_Activity.java
package com.mapsample.msclient009.radiobutton;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  RadioGroup rgp ;
  RadioButton rbtn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
            rgp =(RadioGroup)findViewById ( R.id.RadioGp );
    }

    public void onChecked(View view) {
        int selectedbtn =rgp.getCheckedRadioButtonId ();
        rbtn1 =(RadioButton)findViewById ( selectedbtn );
        if(selectedbtn== -1)
        {
            Toast.makeText (MainActivity.this,"None is selected",Toast.LENGTH_LONG ).show ();
        }
        else
        {Toast.makeText ( MainActivity.this,"You have selected one Button",Toast.LENGTH_LONG ).show ();

        }
    }
}
         Launch Your APP           

                         Radio Button in android


Leave Comment

Comments

Liked By